﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CloudReader.Design.Struct;

namespace CloudReader.Design
{
    public class ColorHelper
    {
        public static System.Windows.Media.Color GetRGB(HSL hsl)
        {
            return hsl.GetRGB();
        }
        public static System.Windows.Media.Color GetRGB(HSV hsv)
        {
            return hsv.GetRGB();
        }
        public static void Compute(int S,out byte R,out byte G,out byte B)
        {
            var L = 1.0f;
            var H = 1.0f;
            float q;
            var half = 1 / 2f;
            if (L < half)
            {
                q = L * (1 + S);
            }
            else
            {
                q = L + S - (L * S);
            }

            float p = 2 * L - q;

            var Hk = H / 360;
            var so = 1 / 3f;
            var tR = Hk + so;
            var tG = Hk;
            var tB = Hk - so;

            if (tR < 0)
            {
                tR += 1.0f;
            }
            else if (tR > 1)
            {
                tR -= 1.0f;
            }

            if (tG < 0)
            {
                tG += 1.0f;
            }
            else if (tG > 1)
            {
                tG -= 1.0f;
            }

            if (tB < 0)
            {
                tB += 1.0f;
            }
            else if (tB > 1)
            {
                tB -= 1.0f;
            }


            if (tR < (1 / 6f))
            {
                R = Convert.ToByte(p + ((q - p) * 6 * tR));
            }
            else if ((1 / 6f) <= tR && tR < (1 / 2f))
            {
                R = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tR && tR < (2 / 3f))
            {
                R = Convert.ToByte(p + ((q - p) * 6 * ((2 / 3f) - tR)));
            }
            else
            {
                R = Convert.ToByte(p);
            }

            if (tG < (1 / 6f))
            {
                G = Convert.ToByte(p + ((q - p) * 6 * tG));
            }
            else if ((1 / 6f) <= tG && tG < (1 / 2f))
            {
                G = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tG && tG < (2 / 3f))
            {
                G = Convert.ToByte(p + ((q - p) * 6 * ((2 / 3f) - tG)));
            }
            else
            {
                G = Convert.ToByte(p);
            }

            if (tB < (1 / 6f))
            {
                B = Convert.ToByte(p + ((q - p) * 6 * tB));
            }
            else if ((1 / 6f) <= tB && tB < (1 / 2f))
            {
                B = Convert.ToByte(q);
            }
            else if ((1 / 2f) <= tB && tB < (2 / 3f))
            {
                B = Convert.ToByte(p + ((q - p) * 6 * ((2 / 3f) - tB)));
            }
            else
            {
                B = Convert.ToByte(p);
            }
        }
    }
}
